/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project3;

import java.util.*;
import javax.xml.stream.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;

/**
 *
 * @author bruesb00
 */
public class ReportGenerators {

    XMLOutputFactory factory;
    XMLStreamWriter xmlWriter;
    PrintWriter writer;
    RetrieveDatabaseInfo database;




    ReportGenerators(){
        factory = XMLOutputFactory.newInstance();
        database = new RetrieveDatabaseInfo();
    }

    /**
    * createOptionOneSampleReport - Creates a sample report using Report Option 1 and outputs it in a JTextArea
    * @param JTextArea The JTextArea the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionOneSampleReport(JTextArea textArea, Timestamp startDate, Timestamp endDate){
	
        textArea.setText("Description:  How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the name of the algorithm for which a visualization was launched.\n");

        try{
	    database.createConnection();
            database.getOptionOneOrThreeSampleCountry(textArea, startDate, endDate, 1);
	    database.closeConnection();
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

    /**
    * createOptionTwoSampleReport - Creates a sample report using Report Option 2 and outputs it in a JTextArea
    * @param JTextArea The JTextArea the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionTwoSampleReport(JTextArea textArea, Timestamp startDate, Timestamp endDate){
        textArea.setText("Description:  How many visualizations were viewed grouped first by the name of the algorithm and, within each algorithm, by country and city.\n");

        try{
	    database.createConnection();
            database.getOptionTwoSampleAlgorithm(textArea, startDate, endDate);
	    database.closeConnection();
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

    /**
    * createOptionThreeSampleReport - Creates a sample report using Report Option 3 and outputs it in a JTextArea
    * @param JTextArea The JTextArea the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionThreeSampleReport(JTextArea textArea, Timestamp startDate, Timestamp endDate){
        textArea.setText("Description:  How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the username of the viewer.\n");

        try{
	    database.createConnection();
            database.getOptionOneOrThreeSampleCountry(textArea, startDate, endDate, 3);
	    database.closeConnection();
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

    /**
    * createOptionOneTextFileReport - Creates a text file report using Report Option 1.
    * @param File The File the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionOneTextFileReport(File file, Timestamp startDate, Timestamp endDate){
        try{
            Calendar currentDate = Calendar.getInstance();

            writer = new PrintWriter(file);
            writer.println("File Name: " + file.getName());
            writer.println("Report Generated on: " + currentDate.getTime());
            writer.println("Description:  How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the name of the algorithm for which a visualization was launched.");
            writer.println("Searching Records After: " + startDate.toString());
            writer.println("Searching Records Before: " + endDate.toString());
            writer.println("");
            writer.println("");
	    database.createConnection();
            database.getOptionOneOrThreeTextFileCountry(writer, startDate, endDate, 1);
	    database.closeConnection();
            writer.close();
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }


    /**
    * createOptionTwoTextFileReport - Creates a text file report using Report Option 2.
    * @param File The File the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionTwoTextFileReport(File file, Timestamp startDate, Timestamp endDate){
        try{
            Calendar currentDate = Calendar.getInstance();

            writer = new PrintWriter(file);
            writer.println("File Name: " + file.getName());
            writer.println("Report Generated on: " + currentDate.getTime());
            writer.println("Description:  How many visualizations were viewed grouped first by the name of the algorithm and, within each algorithm, by country and city.");
            writer.println("Searching Records After: " + startDate.toString());
            writer.println("Searching Records Before: " + endDate.toString());
            writer.println("");
            writer.println("");
	    database.createConnection();
            database.getOptionTwoTextFileAlgorithm(writer, startDate, endDate);
	    database.closeConnection();
            writer.close();
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

    /**
    * createOptionThreeTextFileReport - Creates a text file report using Report Option 3.
    * @param File The File the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionThreeTextFileReport(File file, Timestamp startDate, Timestamp endDate){
        try{
            Calendar currentDate = Calendar.getInstance();

            writer = new PrintWriter(file);
            writer.println("File Name: " + file.getName());
            writer.println("Report Generated on: " + currentDate.getTime());
            writer.println("Description:  How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the username of the viewer.");
            writer.println("Searching Records After: " + startDate.toString());
            writer.println("Searching Records Before: " + endDate.toString());
            writer.println("");
            writer.println("");
	    database.createConnection();
            database.getOptionOneOrThreeTextFileCountry(writer, startDate, endDate, 3);
	    database.closeConnection();
            writer.close();
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

    /**
    * createOptionOneXMLReport - Creates a XML file report using Report Option 1.
    * @param String The file name if the XML file that the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionOneXMLReport(String fileName, Timestamp startDate, Timestamp endDate){
        try{
            Calendar currentDate = Calendar.getInstance();
            File file = new File(fileName);

            xmlWriter = factory.createXMLStreamWriter(new FileOutputStream(fileName));
            xmlWriter.writeStartDocument("1.0");
            xmlWriter.writeStartElement("GeneratedReport");
            
            xmlWriter.writeStartElement("FileName");
            xmlWriter.writeCharacters(file.getName());
            xmlWriter.writeEndElement();
            
            xmlWriter.writeStartElement("DateCreated");
            xmlWriter.writeCharacters(currentDate.getTime().toString()) ;
            xmlWriter.writeEndElement();
            
            xmlWriter.writeStartElement("Description");
            xmlWriter.writeCharacters("How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the name of the algorithm for which a visualization was launched.") ;
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("BeginningDate");
            xmlWriter.writeCharacters(startDate.toString());
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("EndingDate");
            xmlWriter.writeCharacters(endDate.toString());
            xmlWriter.writeEndElement();
            
	    database.createConnection();
            database.getOptionOneOrThreeXMLCountry(xmlWriter, startDate, endDate, 1);
	    database.closeConnection();
            
            xmlWriter.writeEndElement();
            
            xmlWriter.writeEndDocument() ;

            xmlWriter.flush( ) ;
            xmlWriter.close( ) ;
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(javax.xml.stream.XMLStreamException ex){
            System.out.println("XML Stream Exception");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }

    }


    /**
    * createOptionTwoXMLReport - Creates a XML file report using Report Option 2.
    * @param String The file name if the XML file that the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionTwoXMLReport(String fileName, Timestamp startDate, Timestamp endDate){

        try{
            Calendar currentDate = Calendar.getInstance();
            File file = new File(fileName);

            xmlWriter = factory.createXMLStreamWriter(new FileOutputStream(fileName));
            xmlWriter.writeStartDocument("1.0");
            xmlWriter.writeStartElement("GeneratedReport");

            xmlWriter.writeStartElement("FileName");
            xmlWriter.writeCharacters(file.getName());
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("DateCreated");
            xmlWriter.writeCharacters(currentDate.getTime().toString()) ;
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Description");
            xmlWriter.writeCharacters("How many visualizations were viewed grouped first by the name of the algorithm and, within each algorithm, by country and city.");
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("BeginningDate");
            xmlWriter.writeCharacters(startDate.toString());
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("EndingDate");
            xmlWriter.writeCharacters(endDate.toString());
            xmlWriter.writeEndElement();

	    database.createConnection();
            database.getOptionTwoXMLAlgorithm(xmlWriter, startDate, endDate);
	    database.closeConnection();

            xmlWriter.writeEndElement();

            xmlWriter.writeEndDocument() ;

            xmlWriter.flush( ) ;
            xmlWriter.close( ) ;
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(javax.xml.stream.XMLStreamException ex){
            System.out.println("XML Stream Exception");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }


    /**
    * createOptionThreeXMLReport - Creates a XML file report using Report Option 3.
    * @param String The file name if the XML file that the results will be output to.
    * @param Timestamp The Timestamp that represents the starting date of the search.
    * @param Timestamp The Timestamp that represents the ending date of the search.
    */
    public void createOptionThreeXMLReport(String fileName, Timestamp startDate, Timestamp endDate){

        try{
            Calendar currentDate = Calendar.getInstance();
            File file = new File(fileName);

            xmlWriter = factory.createXMLStreamWriter(new FileOutputStream(fileName));
            xmlWriter.writeStartDocument("1.0");
            xmlWriter.writeStartElement("GeneratedReport");

            xmlWriter.writeStartElement("FileName");
            xmlWriter.writeCharacters(file.getName());
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("DateCreated");
            xmlWriter.writeCharacters(currentDate.getTime().toString()) ;
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("Description");
            xmlWriter.writeCharacters("How many visualizations were viewed, grouped first by country, then by city within that country, and finally by the username of the viewer.");
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("BeginningDate");
            xmlWriter.writeCharacters(startDate.toString());
            xmlWriter.writeEndElement();

            xmlWriter.writeStartElement("EndingDate");
            xmlWriter.writeCharacters(endDate.toString());
            xmlWriter.writeEndElement();

	    database.createConnection();
            database.getOptionOneOrThreeXMLCountry(xmlWriter, startDate, endDate, 3);
	    database.closeConnection();

            xmlWriter.writeEndElement();

            xmlWriter.writeEndDocument() ;

            xmlWriter.flush( ) ;
            xmlWriter.close( ) ;
        }
        catch(java.io.FileNotFoundException ex){
            System.out.println("File not Found");
        }
        catch(javax.xml.stream.XMLStreamException ex){
            System.out.println("XML Stream Exception");
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }


    /**
    * deleteRecords - Deletes all records before a certain date.
    * @param Timestamp The Timestamp that represents the date.  All records before that date will be deleted.
    */
    public void deleteRecords(Timestamp deleteDate){
        try{
	    database.createConnection();
            database.deleteRecords(deleteDate);
	    database.closeConnection();
        }
        catch(java.sql.SQLException ex){
            System.out.println("SQL error");
        }
    }

}
